package com.service.single.controller;


import com.service.single.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 文件操作
 */
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 上传文件
     */
    @PostMapping("/uploadFile")
    public void uploadFile(@RequestParam MultipartFile file) {
        fileService.uploadFile(file);
    }

    /**
     * 批量上传
     */
    @PostMapping("/uploadBatchFiles")
    public void uploadBatchFiles(@RequestParam MultipartFile[] files) {
        fileService.uploadBatchFiles(files);
    }

    /**
     * 下载
     *
     * @param id 主键
     */
    @GetMapping("/downloadFile/{id}")
    public void downloadFile(@PathVariable("id") Long id) {
        fileService.downloadFile(id);
    }

    /**
     * 删除文件
     *
     * @param id 主键
     */
    @DeleteMapping("/deleteFile/{id}")
    public void deleteFile(@PathVariable("id") Long id) {
        fileService.deleteFile(id);
    }

    /**
     * 分片上传文件
     *
     * @param file        文件
     * @param chunkNumber 分片文件下标
     * @param totalChunks 分片总数
     * @param identifier  分片文件标识
     * @return
     */
    @PostMapping("/uploadChunk")
    public Map<String, Object> uploadChunk(@RequestParam("file") MultipartFile file, @RequestParam("chunkNumber") int chunkNumber,
                                           @RequestParam("totalChunks") int totalChunks, @RequestParam("identifier") String identifier) {
        return fileService.uploadChunk(file, chunkNumber, totalChunks, identifier);

    }

    /**
     * 合并分片文件
     *
     * @param identifier  分片文件标识
     * @param totalChunks 分片总数
     * @return
     */
    @PostMapping("/mergeChunks")
    public Map<String, Object> mergeChunks(@RequestParam("identifier") String identifier,
                                           @RequestParam("totalChunks") int totalChunks) {
        return fileService.mergeChunks(identifier, totalChunks);
    }
}
